package com.inei.reportedeverificacion.model.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inei.reportedeverificacion.model.connection.UtilDB;
import com.inei.reportedeverificacion.model.entity.ReporteCap2;
import com.inei.reportedeverificacion.model.entity.ReporteCap2Tmp;

public class ReporteCap2TmpDao implements ICRUD<ReporteCap2Tmp> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	// constructor
	public ReporteCap2TmpDao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public ReporteCap2Tmp buscar(ReporteCap2Tmp o) {
		ReporteCap2Tmp obj = null;
		db = util.getReadableDatabase();
		cursor = db
				.rawQuery(
						"select * from ReporteCap2Tmp where CLAVEVIVHOGAR=? and NRO_ORDEN=?",
						new String[] { o.getCLAVEVIVHOGAR() + "",
								"" + o.getNRO_ORDEN() });
		if (cursor.moveToFirst()) {
			obj = new ReporteCap2Tmp();
			obj.setCLAVEVIVHOGAR(cursor.getString(cursor
					.getColumnIndex("CLAVEVIVHOGAR")));
			obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
			obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
			obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
			obj.setIdgrupo(cursor.getString(cursor.getColumnIndex("idgrupo")));
			obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
			obj.setNRO_ORDEN(cursor.getInt(cursor.getColumnIndex("NRO_ORDEN")));
			obj.setC1P21A(cursor.getString(cursor.getColumnIndex("C1P21A")));
			obj.setC1P21B(cursor.getString(cursor.getColumnIndex("C1P21B")));
			obj.setC1P21C(cursor.getString(cursor.getColumnIndex("C1P21C")));
			obj.setC1P22(cursor.getInt(cursor.getColumnIndex("C1P22")));
			obj.setC1P24(cursor.getInt(cursor.getColumnIndex("C1P24")));
			obj.setC1P26_ANIO(cursor.getString(cursor
					.getColumnIndex("C1P26_ANIO")));
			obj.setC1P26_MESES(cursor.getString(cursor
					.getColumnIndex("C1P26_MESES")));
			obj.setC1P27_DIA(cursor.getString(cursor
					.getColumnIndex("C1P27_DIA")));
			obj.setC1P27_MES(cursor.getString(cursor
					.getColumnIndex("C1P27_MES")));
			obj.setC1P27_ANIO(cursor.getString(cursor
					.getColumnIndex("C1P27_ANIO")));
		}
		Cerrar();
		return obj;
	}

	public List<ReporteCap2Tmp> leerXHOGAR(String id) {
		List<ReporteCap2Tmp> l = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from ReporteCap2Tmp where CLAVEVIVHOGAR=?",
				new String[] { id + "" });
		if (cursor.moveToFirst()) {
			l = new ArrayList<ReporteCap2Tmp>();
			do {
				ReporteCap2Tmp obj = new ReporteCap2Tmp();
				obj.setCLAVEVIVHOGAR(cursor.getString(cursor
						.getColumnIndex("CLAVEVIVHOGAR")));
				obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
				obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
				obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
				obj.setIdgrupo(cursor.getString(cursor
						.getColumnIndex("idgrupo")));
				obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
				obj.setNRO_ORDEN(cursor.getInt(cursor
						.getColumnIndex("NRO_ORDEN")));
				obj.setC1P21A(cursor.getString(cursor.getColumnIndex("C1P21A")));
				obj.setC1P21B(cursor.getString(cursor.getColumnIndex("C1P21B")));
				obj.setC1P21C(cursor.getString(cursor.getColumnIndex("C1P21C")));
				obj.setC1P22(cursor.getInt(cursor.getColumnIndex("C1P22")));
				obj.setC1P24(cursor.getInt(cursor.getColumnIndex("C1P24")));
				obj.setC1P26_ANIO(cursor.getString(cursor
						.getColumnIndex("C1P26_ANIO")));
				obj.setC1P26_MESES(cursor.getString(cursor
						.getColumnIndex("C1P26_MESES")));
				obj.setC1P27_DIA(cursor.getString(cursor
						.getColumnIndex("C1P27_DIA")));
				obj.setC1P27_MES(cursor.getString(cursor
						.getColumnIndex("C1P27_MES")));
				obj.setC1P27_ANIO(cursor.getString(cursor
						.getColumnIndex("C1P27_ANIO")));
				l.add(obj);
			} while (cursor.moveToNext());
		}
		Cerrar();
		return l;
	}

	@Override
	public int crear(ReporteCap2Tmp o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues valores = new ContentValues();
		valores.put("CLAVEVIVHOGAR", o.getCLAVEVIVHOGAR());
		valores.put("ccdd", o.getCcdd());
		valores.put("ccpp", o.getCcpp());
		valores.put("ccdi", o.getCcdi());
		valores.put("idgrupo", o.getIdgrupo());
		valores.put("HOGAR", o.getHOGAR());
		valores.put("NRO_ORDEN", o.getNRO_ORDEN());
		valores.put("C1P21A", o.getC1P21A());
		valores.put("C1P21B", o.getC1P21B());
		valores.put("C1P21C", o.getC1P21C());
		valores.put("C1P22", o.getC1P22());
		valores.put("C1P24", o.getC1P24());
		valores.put("C1P26_ANIO", o.getC1P26_ANIO());
		valores.put("C1P26_MESES", o.getC1P26_MESES());
		valores.put("C1P27_DIA", o.getC1P27_DIA());
		valores.put("C1P27_MES", o.getC1P27_MES());
		valores.put("C1P27_ANIO", o.getC1P27_ANIO());

		resultado = (int) db.insert("ReporteCap2Tmp", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}
		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(ReporteCap2Tmp o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(ReporteCap2Tmp o) {
		// TODO Auto-generated method stub
		return 0;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		db.close();
	}

	@Override
	public List<ReporteCap2Tmp> leer() {
		// TODO Auto-generated method stub
		return null;
	}
}
